package com.hanhanxiaochong.zhongxiangchuanmei.view.home.act;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.util.MessageEvent;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 预约时间
 */
public class AppointmentTimeActivity extends BaseActivity implements DatePicker.OnDateChangedListener, TimePicker.OnTimeChangedListener {

    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.tv_start_date)
    TextView tvStartDate;
    @BindView(R.id.ll_start_date)
    LinearLayout llStartDate;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.ll_start_time)
    LinearLayout llStartTime;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.date_picker)
    DatePicker datePicker;
    @BindView(R.id.time_picker)
    TimePicker timePicker;

    private String initDate;

    public static Intent createIntent(Context context) {
        return new Intent(context, AppointmentTimeActivity.class);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_appointment_time);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void init() {
        Calendar calendar = Calendar.getInstance();
        setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePicker.setMinDate(calendar.getTimeInMillis());
        initDataPicker(datePicker);

        timePicker.setOnTimeChangedListener(this);
    }

    @OnClick({R.id.ll_leftview, R.id.ll_start_date, R.id.ll_start_time, R.id.tv_confirm})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.ll_start_date:
                timePicker.setVisibility(View.GONE);
                datePicker.setVisibility(View.VISIBLE);
                break;
            case R.id.ll_start_time:
                datePicker.setVisibility(View.GONE);
                timePicker.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_confirm:
                String date = tvStartDate.getText().toString();
                String time = tvStartTime.getText().toString();
                if (!date.equals("") && !time.equals("")) {
                    MessageEvent messageEvent = new MessageEvent(date + " " + time);
                    messageEvent.setFrom("appointment");
                    EventBus.getDefault().post(messageEvent);
                    finish();
                } else {
                    ToastUtils.showToast("时间不能为空");
                }
                break;
        }
    }

    //初始化DatePicker
    private void initDataPicker(DatePicker datePicker) {
        Calendar calendar = Calendar.getInstance();
        if (!(null == initDate || "".equals(initDate))) {
            calendar = getCalendar(initDate);
        } else {
            initDate = calendar.get(Calendar.YEAR) + "-"
                    + calendar.get(Calendar.MONTH) + "-"
                    + calendar.get(Calendar.DAY_OF_MONTH);
        }
        datePicker.init(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) - 1,
                calendar.get(Calendar.DAY_OF_MONTH), this);
    }

    //将字符串YYYY-MM-DD变成年月日
    private Calendar getCalendar(String initDate) {
        Calendar calendar = Calendar.getInstance();
        String s[] = initDate.split("-");
        calendar.set(Integer.valueOf(s[0]), Integer.valueOf(s[1]), Integer.valueOf(s[2]));
        return calendar;
    }


    public static void setDatePickerDividerColor(DatePicker datePicker) {
        LinearLayout llFirst = (LinearLayout) datePicker.getChildAt(0);
        // 获取 NumberPicker
        LinearLayout mSpinners = (LinearLayout) llFirst.getChildAt(0);
        for (int i = 0; i < mSpinners.getChildCount(); i++) {
            NumberPicker picker = (NumberPicker) mSpinners.getChildAt(i);
            Field[] pickerFields = NumberPicker.class.getDeclaredFields();
            for (Field pf : pickerFields) {
                if (pf.getName().equals("mSelectionDivider")) {
                    pf.setAccessible(true);
                    try {
                        pf.set(picker, new ColorDrawable());
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (Resources.NotFoundException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

    public static void setTimePickerDividerColor(TimePicker timePicker) {
        // 获取 mSpinners
        LinearLayout llFirst = (LinearLayout) timePicker.getChildAt(0);
        //获取 NumberPicker
        LinearLayout mSpinners = (LinearLayout) llFirst.getChildAt(1);
        for (int i = 0; i < mSpinners.getChildCount(); i++) {
            if (mSpinners.getChildAt(i) instanceof NumberPicker) {
                Field[] pickerFields = NumberPicker.class.getDeclaredFields();
                for (Field pf : pickerFields) {
                    if (pf.getName().equals("mSelectionDivider")) {
                        pf.setAccessible(true);
                        try {
                            pf.set(mSpinners.getChildAt(i), new ColorDrawable());
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (Resources.NotFoundException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        setDate(year, monthOfYear, dayOfMonth);
    }

    @SuppressLint("SimpleDateFormat")
    private void setDate(int year, int monthOfYear, int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, monthOfYear,
                dayOfMonth);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(calendar.getTime());
        tvStartDate.setText(date);
    }

    @SuppressLint("SimpleDateFormat")
    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(0, 0, 0, hourOfDay, minute);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String time = sdf.format(calendar.getTime());
        tvStartTime.setText(time);
    }
}
